let app = getApp();
const previewUtil = require("./../../utils/preview.js");
import wxRequest from "../../utils/http.js"
import Notify from "/@vant/weapp/notify/notify";
import Dialog from '/@vant/weapp/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    allDynamic: [],
    dynamicId: "",
    allHeadPortrait: [],
    imagesUrl: [],
    active: 0,
    show: 0,
    hidden: false,
    // 展示评论输入框
    showCommentInput: false,
    // 展示回复评论输入框
    showReplyCommentInput: false,
    // 评论id
    commentId: '',
    // 评论内容
    commentContent: "",
    // 父级评论id
    parentCommentId: "",
    // 父级评论者id
    parentCommenterId: "",
    // 父级评论者昵称
    parentCommenterNickName: "",
    // 回复父级评论id
    replyCommentId: "",
    // 回复输入内容
    replyCommentContent: "",
    toCopyCommentContent: "",
    commentCanDelete: '',
    showPopup: false,
    showDeletePopup: false,
    noCommentShow: false,
    actions: [
      { name: '评论', key: '1' }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "动态详情",
    });
    this.setData({
      dynamicId: options.dynamicId,
    });
    this.getDynamicByDynamicId();
  },
 /**
   * 根据动态id获取动态详情
   */
  getDynamicByDynamicId() {
    let that = this;
    wx.request({
      url:
        app.globalData.baseUrl +
        `/dynamic/findDynamicById?dynamicId=` +
        this.data.dynamicId,
      method: "GET",
      success(res) {
        that.setData({
          allDynamic: res.data.result[0],
        });
      },
    });
  },
  /**
   * 预览单张图片
   * @param {*} event 
   */
  toPreviewImage(event) {
    previewUtil.previewImage(event);
  },
  /**
   * 预览多张图片
   * @param {*} event 
   */
  toPreviewMoreImage(event) {
    previewUtil.previewMoreImage(event);
  },
  /**
   * 将微信小程序的头像显示为高清
   * @param {*} event 
   */
  toPreviewWeChatAvatarUrlImage(event) {
    previewUtil.previewWeChatAvatarUrlImage(event);
  },
  /**
   * 触摸屏幕后移动触发一些隐藏操作
   */
  hiddenComment: function () {
    this.setData({
      show: 0,
      hidden: false,
      showCommentInput: false,
      showReplyCommentInput: false,
    });
  },
  /**
   * 激活评论框
   */
  showCommentInput: function (event) {
    // 先判断有没有登录
    if (wx.getStorageSync('token') === null || wx.getStorageSync('token') === '') {
      Dialog.alert({
        title: '未授权或未登录',
        message: '请先授权登录'
      }).then(() => {
      });
    } else {
      // 登录过了
      this.setData({
        show: 0,
        hidden: false,
        showReplyCommentInput: false,
        // 展示评论输入框
        showCommentInput: true,
      });
    }
  },
  /**
   * 获取评论框的输入内容
   */
  getCommentContent: function (event) {
    let content = event.detail.value;
    this.setData({
      commentContent: "",
    });
    this.setData({
      commentContent: content,
    });
  },
  /**
   * 发评论
   */
  sendComment() {
    let that = this;
    wx.request({
      url: app.globalData.baseUrl + `/dynamic-comments/writeComments`,
      method: "POST",
      data: {
        dynamicId: that.data.dynamicId,
        commentContent: that.data.commentContent,
        commenterAvatarUrl: wx.getStorageSync("avatarUrl"),
      },
      header: {
        token: wx.getStorageSync("token"),
        "Content-Type": "application/json",
      },
      success: function (res) {
        wx.showLoading({
          title: "发送中",
        });
        if (res.data.code === 2003) {
          wx.showToast({
            title: "评论成功",
            icon: "none",
          });
        }
        that.setData({
          show: 0,
          hidden: false,
          showCommentInput: false,
        });
        that.getDynamicByDynamicId();
      },
    });
  },
  /**
   * 展示回复评论框
   * @param event
   */
  showReplyCommentInput(event) {

    this.setData({
      show: 0,
      hidden: false,
      showCommentInput: false,
      showReplyCommentInput: true,
      showPopup: false
    });

  },
  /**
   * 判断是否能删除这条评论
   */
  checkWhetherCanDeleteThisComments(e) {
    if (wx.getStorageSync('token') === null || wx.getStorageSync('token') === '') {
      Dialog.alert({
        title: '未授权或未登录',
        message: '请先授权登录'
      }).then(() => {
      });
    } else {
      this.setData({
        showPopup: true
      });
      let commentId = e.currentTarget.dataset.commentid;
      this.setData({
        commentId: commentId
      });
      let that = this;
      wx.request({
        url: app.globalData.baseUrl + `/dynamic-comments/whetherCanDeleteThisComments?commentId=` + commentId,
        method: "GET",
        header: {
          token: wx.getStorageSync("token"),
        },
        success: function (res) {
          that.setData({
            toCopyCommentContent: res.data.result.commentContent
          })
          that.setData({
            commentCanDelete: res.data.result.commentCanDelete
          })
        },
      })
    }
  },
  onClose() {
    this.setData({
      showPopup: false
    });
  },
  deletePopup() {
    this.setData({
      showPopup: false,
      showDeletePopup: true
    });
  },
  /**
   * 取消删除的弹出框
   */
  onDeleteClose() {
    this.setData({
      showDeletePopup: false
    });
  },
  /**
   * 删除评论
   */
  deleteComment() {
    let that = this;
    wx.request({
      url: app.globalData.baseUrl + `/dynamic-comments/deleteComments`,
      method: "POST",
      data: {
        commentId: that.data.commentId,
        dynamicId: that.data.dynamicId
      },
      header: {
        token: wx.getStorageSync("token"),
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code === 2007) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '删除失败,请联系客服',
            icon: 'none'
          });
        }
        that.setData({
          showDeletePopup: false
        })
        that.getDynamicByDynamicId();
      },
    })
  },
  /**
   * 获取回复评论的内容
   * @param e
   */
  getReplyCommentContent: function (e) {
    let commentContent = e.detail.value;
    this.setData({
      commentContent: "",
    });
    this.setData({
      commentContent: commentContent,
    });
  },
  /**
   * 回复内容
   * @param event
   */
  replyComment(event) {
    // 父级评论id
    this.setData({
      parentCommentId: event.currentTarget.dataset.parentcommentid,
    });
    // 父级评论者id
    this.setData({
      parentCommenterId: event.currentTarget.dataset.parentcommenterid,
    });
    // 父级评论者头像url
    this.setData({
      parentCommenterAvatarUrl:
        event.currentTarget.dataset.parentcommenteravatarurl,
    });
    // 父级评论者昵称
    this.setData({
      parentCommenterNickName:
        event.currentTarget.dataset.parentcommenternickname,
    });
  },
  /**
   * 发送回复评论
   */
  sendReplyComment() {
    let that = this;
    wx.request({
      url: app.globalData.baseUrl + `/dynamic-comments/writeComments`,
      method: "POST",
      data: {
        dynamicId: that.data.dynamicId,
        commentContent: that.data.commentContent,
        commenterAvatarUrl: wx.getStorageSync("avatarUrl"),
        parentCommentId: that.data.parentCommentId,
        parentCommenterId: that.data.parentCommenterId,
        parentCommenterNickName: that.data.parentCommenterNickName,
        parentCommenterAvatarUrl: that.data.parentCommenterAvatarUrl,
      },
      header: {
        token: wx.getStorageSync("token"),
        "Content-Type": "application/json",
      },
      success: function (res) {
        wx.showLoading({
          title: "发送中",
        });
        if (res.data.code === 2003) {
          wx.showToast({
            title: "评论成功",
            icon: "none",
          });
        }
        that.setData({
          show: 0,
          hidden: false,
          showReplyCommentInput: false,
        });
        that.getDynamicByDynamicId();
      },
    });
  },
  /**
   * 复制
   */
  textPaste() {
    wx.showToast({
      title: '复制成功',
    })
    let that = this;
    wx.setClipboardData({
      data: that.data.toCopyCommentContent,
      success: function (res) {
        wx.getClipboardData({    //这个api是把拿到的数据放到电脑系统中的
          success: function (res) {
            that.setData({
              showPopup: false
            })
          }
        })
      }
    })
  },
  // 展示无评论时的弹出框
  commentAction() {
    if (this.data.showCommentInput === false) {
      this.setData({
        noCommentShow: true
      })
    }
  },
  onSelect(e) {
    console.log(e.detail);
    if (e.detail.key === '1') {
      this.setData({
        noCommentShow: false,
        show: 0,
        hidden: false,
        showReplyCommentInput: false,
        showCommentInput: true,
      })
    }
  },
  // 
  cancelNoCommentShow() {
    this.setData({
      noCommentShow: false
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    this.getDynamicByDynamicId();
    wx.showToast({
      title: '刷新成功',
      icon: 'none',
      duration: 2000
    });
    wx.stopPullDownRefresh();
  },
});
